﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections;

namespace Bilyteam_Website
{
    public partial class DichVuCon : System.Web.UI.MasterPage
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable tbGalleries = new DataTable();
        DataTable tbTop = new DataTable();
        public DataTable tbBottom = new DataTable();
        public DataTable tbItems = new DataTable();
        private int countID = 0;
        public ArrayList arrCount = new ArrayList();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Repeater1.DataSource = getMenu();
                Repeater1.DataBind();

                Repeater2.DataSource = getMenu1();
                Repeater2.DataBind();

                Repeater3.DataSource = getMenucon1();
                Repeater3.DataBind();

                Repeater4.DataSource = getMenuVecon1();
                Repeater4.DataBind();

                Repeater5.DataSource = getMenu2();
                Repeater5.DataBind();

                Repeater6.DataSource = getMenucon2();
                Repeater6.DataBind();

                Repeater7.DataSource = getMenuVecon2();
                Repeater7.DataBind();

                Repeater8.DataSource = getMenu3();
                Repeater8.DataBind();

                Repeater9.DataSource = getMenucon3();
                Repeater9.DataBind();

                BindData();
            }
        }

        public DataTable getMenu()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = "select * from MenuItems Where Status = 1";
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }

        public DataTable getMenu1()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = "select * from Categories where CategoryID = '1'";
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }

        public DataTable getMenucon1()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = "select * from Categories where Parrent = 1 and [Type] = '1'";

            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }

        public DataTable getMenuVecon1()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = "select * from Categories where Parrent = 1 and [Type] = '2'";
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }

        public DataTable getMenu2()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = "select * from Categories where CategoryID = '2'";
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }

        public DataTable getMenucon2()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = "select * from Categories where Parrent = 2 and [Type] = '1'";

            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }

        public DataTable getMenuVecon2()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = "select * from Categories where Parrent = 2 and [Type] = '2'";
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }


        public DataTable getMenu3()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = "select * from Categories where CategoryID = '3'";
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }

        public DataTable getMenucon3()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = "select * from Categories where Parrent = 3 and [Type] = '1'";

            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }

        
        private void BindData()
        {
            string sql = "SELECT Images.ImageID, Images.ImageName, Images.Location, Images.Status AS ImagesStatus, Galleries.GalleryID, Galleries.GalleryName, Galleries.GalleryDescription, ";
            sql += "Galleries.Status AS GalleriesStatus, ImageToGallery.ID, ImageToGallery.IDImage, ImageToGallery.IDGallery ";
            sql += "FROM Galleries INNER JOIN ";
            sql += "ImageToGallery ON Galleries.GalleryID = ImageToGallery.IDGallery INNER JOIN ";
            sql += "Images ON ImageToGallery.IDImage = Images.ImageID ";
            sql += "WHERE ImageToGallery.IDGallery = Galleries.GalleryID AND ImageToGallery.IDImage = Images.ImageID AND Galleries.GalleryID = 1 AND Images.Status = 'True' AND Galleries.Status = 'True'";
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
            cmd.Connection = conn;
            cmd.CommandText = sql;
            cmd.CommandType = CommandType.Text;
            da.SelectCommand = cmd;
            da.Fill(ds, "Galleries");
            tbGalleries = ds.Tables["Galleries"];


            DataColumn CountIDItems = new DataColumn("CountID");
            DataColumn ImageIDItems = new DataColumn("ImageID");
            DataColumn ImageNameItems = new DataColumn("ImageName");
            DataColumn LocationItems = new DataColumn("Location");
            DataColumn ImagesStatusItems = new DataColumn("ImagesStatus");
            DataColumn GalleryIDItems = new DataColumn("GalleryID");
            DataColumn GalleryNameItems = new DataColumn("GalleryName");
            DataColumn GalleryDescriptionItems = new DataColumn("GalleryDescription");
            DataColumn GalleriesStatusItems = new DataColumn("GalleriesStatus");
            DataColumn IDItems = new DataColumn("ID");
            DataColumn IDImageItems = new DataColumn("IDImage");
            DataColumn IDGalleryItems = new DataColumn("IDGallery");

            CountIDItems.DataType = System.Type.GetType("System.Int32");
            ImageIDItems.DataType = System.Type.GetType("System.Int32");
            ImageNameItems.DataType = System.Type.GetType("System.String");
            LocationItems.DataType = System.Type.GetType("System.String");
            ImagesStatusItems.DataType = System.Type.GetType("System.Boolean");
            GalleryIDItems.DataType = System.Type.GetType("System.Int32");
            GalleryNameItems.DataType = System.Type.GetType("System.String");
            GalleryDescriptionItems.DataType = System.Type.GetType("System.String");
            GalleriesStatusItems.DataType = System.Type.GetType("System.Boolean");
            IDItems.DataType = System.Type.GetType("System.Int32");
            IDImageItems.DataType = System.Type.GetType("System.Int32");
            IDGalleryItems.DataType = System.Type.GetType("System.Int32");

            tbItems.Columns.Add(CountIDItems);
            tbItems.Columns.Add(ImageIDItems);
            tbItems.Columns.Add(ImageNameItems);
            tbItems.Columns.Add(LocationItems);
            tbItems.Columns.Add(ImagesStatusItems);
            tbItems.Columns.Add(GalleryIDItems);
            tbItems.Columns.Add(GalleryNameItems);
            tbItems.Columns.Add(GalleryDescriptionItems);
            tbItems.Columns.Add(GalleriesStatusItems);
            tbItems.Columns.Add(IDItems);
            tbItems.Columns.Add(IDImageItems);
            tbItems.Columns.Add(IDGalleryItems);

            if (tbGalleries != null)
            {
                for (int i = 0; i < tbGalleries.Rows.Count; i++)
                {
                    if (i % 2 == 0)
                    {
                        countID = i;
                        arrCount.Add(countID);
                    }
                    
                    DataRow row = tbItems.NewRow();
                    row[CountIDItems] = countID;
                    row[ImageIDItems] = int.Parse(tbGalleries.Rows[i]["ImageID"].ToString());
                    row[ImageNameItems] = tbGalleries.Rows[i]["ImageName"].ToString();
                    row[LocationItems] = tbGalleries.Rows[i]["Location"].ToString();
                    row[ImagesStatusItems] = bool.Parse(tbGalleries.Rows[i]["ImagesStatus"].ToString());
                    row[GalleryIDItems] = int.Parse(tbGalleries.Rows[i]["GalleryID"].ToString());
                    row[GalleryNameItems] = tbGalleries.Rows[i]["GalleryName"].ToString();
                    row[GalleryDescriptionItems] = tbGalleries.Rows[i]["GalleryDescription"].ToString();
                    row[GalleriesStatusItems] = bool.Parse(tbGalleries.Rows[i]["GalleriesStatus"].ToString());
                    row[IDItems] = int.Parse(tbGalleries.Rows[i]["ID"].ToString());
                    row[IDImageItems] = int.Parse(tbGalleries.Rows[i]["IDImage"].ToString());
                    row[IDGalleryItems] = int.Parse(tbGalleries.Rows[i]["IDGallery"].ToString());
                    tbItems.Rows.Add(row);
                    
                }
            }

            
        }
    }
}